package com.zjj.algorithm.learning.atguigu.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * 弗洛伊德算法求解最短路劲
 *
 * @author zjj_admin
 * @date 2023/1/5 20:24
 */
public class FloydAlgorithm {

    public static void main(String[] args) {

        char[] vertex = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int[][] weight = new int[][]{
                {-1, 9, -1, -1, -1, 5, 8},
                {9, -1, 6, -1, -1, 3, -1},
                {-1, 6, -1, 4, 2, 8, -1},
                {-1, -1, 4, -1, 12, -1, -1},
                {-1, -1, 2, 12, -1, 3, 12},
                {5, 3, -1, -1, 3, -1, 10},
                {8, -1, -1, -1, 12, 10, -1}
        };
        MinTree minTree = new MinTree();
        FloydPassage passage = new FloydPassage(vertex, weight);
        minTree.floydAlgorithm(passage);
        //最短距离数组
        int[][] dist = passage.dist;
        Map<Character, Integer> pIndex = passage.pointIndex;
        System.out.println(dist[pIndex.get('A')][pIndex.get('D')]);

    }





}

/**
 * 弗洛伊德 算法获取最短路劲的中间类
 */
class FloydPassage {

    /**
     * 保存各个顶点到其他顶点的距离【会动态变化】
     * dist[2][3] = 6 表示在节点数组中索引为 2 的节点到索引为 3 的节点之间的最小距离 为 5
     */
    int[][] dist;

    /**
     * 保存到达目标顶点的前驱节点，其中索引，只有在求路径路线才会使用到
     */
    char[][] pre;

    /**
     * 每一个顶点在顶点数组中的 序号
     */
    Map<Character, Integer> pointIndex = new HashMap<>();

    /**
     * 构造方法
     *
     * @param vertex 图的节点数据
     * @param weight 图的边
     */
    public FloydPassage(char[] vertex, int[][] weight) {
        //初始化
        this.dist = new int[vertex.length][vertex.length];
        this.pre = new char[vertex.length][vertex.length];
        //封装节点的索引
        for (int i = 0; i < vertex.length; i++) {
            pointIndex.put(vertex[i], i);
        }
        for (int i = 0; i < weight.length; i++) {
            for (int j = 0; j < weight[i].length; j++) {
                this.dist[i][j] = weight[i][j];
                this.pre[i][j] = vertex[i];
            }
        }
        System.out.println();
    }

}
